package com.Socket.tongxinlx;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTCp04 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务器端 在9999端口，正在监听");

        Socket socket = serverSocket.accept();
        System.out.println("服务端："+socket.getClass());

        InputStream inputStream = socket.getInputStream();
//        byte[] bytes = new byte[1024];
//        int readLen = 0;
//        while ((readLen = inputStream.read(bytes)) != -1){
//            System.out.println(new String(bytes,0,readLen));
//        }
        //使用字符流(将input。。转为字符流)
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        System.out.println(s);

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello,sock zifu");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        bufferedWriter.close();
//        outputStream.close();
//        inputStream.close();
        bufferedReader.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务端结束会与");
    }
}
